<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item>
      <label for="fileInput" class="file-upload">
        <span class="file-upload-label">选择头像 </span>
        <input
          id="fileInput"
          type="file"
          ref="fileInput"
          @change="handleFileChange"
        />
      </label>
    </el-form-item>
    <el-form-item label="名称" prop="nickname">
      <el-input v-model="ruleForm.nickname"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="Name">
      <el-input v-model.number="ruleForm.Name"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input
        type="password"
        v-model="ruleForm.pass"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input
        type="password"
        v-model="ruleForm.checkPass"
        autocomplete="off"
      ></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getNew } from "../../../api/data";
export default {
  props: {
    isShow: Boolean,
  },
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      setTimeout(() => {
        if (Number.isInteger(value)) {
          callback(new Error("请输入不能是纯数字值"));
        } else {
          callback();
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      form: {},
      ruleForm: {
        image: null,
        pass: "",
        checkPass: "",
        Name: "",
        nickname: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        Name: [{ validator: checkName, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const formData = new FormData();
          formData.append("username", this.ruleForm.Name);
          formData.append("password", this.ruleForm.pass);
          formData.append("nickname", this.ruleForm.nickname);
          formData.append("image", this.ruleForm.image);
          getNew(formData).then(({ data: res }) => {
            if (res == "注册成功") {
              this.$message({
                message: "注册成功",
                type: "success",
              });
              this.$emit("submitForm", false);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      console.log(this.isShow);
      this.$refs[formName].resetFields();
    },
    handleFileChange(event) {
      const file = this.$refs.fileInput.files[0];

      // 这里可以对选中的文件进行其他处理
      this.ruleForm.image = file;
    },
  },
};
</script>
<style>
.file-upload {
  display: inline-block;
  position: relative;
  overflow: hidden;
  margin: 10px;
}

.file-upload input[type="file"] {
  position: absolute;
  font-size: 100px;
  opacity: 0;
  top: 0;
  right: 0;
}

.file-upload-label {
  background-color: #3498db;
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
}
</style>